#include <stdio.h>
#include <string.h>
#include <stddef.h>

#include "sqlite3.h"

int sqlite_select(sqlite3  **db, char  **dbResult)
{
	int                     nRow = 0;
	int                     nColumn = 0;
    int                     r = 0;
	int                     i = -1;
	int                     j = -1;
	int                     rv = -1;
	int                     index = -1;
	char                    *errmsg = NULL;
    //char                    **dbResult = NULL;
	char                    sql[1024] = {};
	char                    sele_data[256] = {};

	memset(sql, 0, sizeof(sql));
	sprintf(sql,"select *from temp");

	rv = sqlite3_get_table(*db, sql, &dbResult, &nRow, &nColumn, &errmsg);
	if(rv!=SQLITE_OK)
	{
		printf("error:%s\n", errmsg);
		return -1;
	}
	printf("select all rv=%d\n", rv);/////////////////////////////////////////////////
	printf("select all nRow=%d\n", nRow);/////////////////////////////////////////////////

	while(nRow > 0)
	{
		memset(sql, 0, sizeof(sql));
		sprintf(sql,"select *from temp where ID=0");

		rv = sqlite3_get_table(*db, sql, &dbResult, &nRow, &nColumn, &errmsg);
		if(rv!=SQLITE_OK)
		{
			printf("error:%s\n", errmsg);
			return -1;
		}
		if(nRow > 0)
		{
			index = nColumn;
			for(i=0;i<nRow;i++)
			{
				for(j=0;j<nColumn;j++)
				{
					//printf("%s %s\n",dbResult[j],dbResult[index]);
					printf("%s\n",dbResult[j][index]);
					index++;
				}
			}
		}
		//printf("select limit dbResult=%s %s\n", dbResult[j],dbResult[index]);/////////////////////////////////////////////////
		printf("select limit dbResult=%s %s\n", dbResult[j][index]);/////////////////////////////////////////////////
		printf("select limit rv=%d\n", rv);/////////////////////////////////////////////////
	}
	
	memset(sql, 0, sizeof(sql));
	sprintf(sql,"delete from temp where ID=0");

	rv = sqlite3_exec(*db, sql, NULL, NULL, &errmsg);
	if(rv!=SQLITE_OK)
	{
		printf("error:%s\n", errmsg);
		return -1;
	}
	printf("delete limit rv=%d\n", rv);/////////////////////////////////////////////////
	nRow-=1;
	if(nRow <= 0)
	{
		r = 2;
	}
	printf("nRow while =%d\n", nRow);//////////////////////////////////////////////////

	return r;
}

